package com.qzsoft.system.modules.permission.dept.service.impl;

import com.qzsoft.jeemis.common.validator.AssertUtils;
import com.qzsoft.system.modules.permission.dept.dto.SysDeptDTO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TreeUtils {
    public TreeUtils() {
    }

    public static  List<SysDeptDTO> build(List<SysDeptDTO> treeNodes, Object pcode) {
        AssertUtils.isNull(pcode, new String[]{"pcode"});
        List<SysDeptDTO> treeList = new ArrayList();
        Iterator var3 = treeNodes.iterator();

        while(var3.hasNext()) {
            SysDeptDTO treeNode = (SysDeptDTO)var3.next();
            if (pcode.equals(treeNode.getPid())) {
                treeList.add(findChildren(treeNodes, treeNode));
            }
        }

        return treeList;
    }

    private static  SysDeptDTO findChildren(List<SysDeptDTO> treeNodes, SysDeptDTO rootNode) {
        Iterator var2 = treeNodes.iterator();
        while(var2.hasNext()) {
            SysDeptDTO treeNode = (SysDeptDTO)var2.next();
            if (rootNode.getId().equals(treeNode.getPid())) {
                SysDeptDTO children = findChildren(treeNodes, treeNode);
                if (children != null && rootNode.getChildren() == null) {
                    rootNode.setChildren(new ArrayList());
                }

                rootNode.getChildren().add(children);
            }
        }

        return rootNode;
    }

}
